<?php
class Menurole_model extends TP_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->table = 'rel_role_menu';
    }

    /**
     * getMenusByRole function
     *
     * @Name getMenusByRole
     * @Desc 获取当前角色菜单权限(兼容多角色问题)
     * @Author Pekey
     * @Date 2020-08-26
     * @param [type] $role
     * @return void
     */
    public function getMenusByRole($role)
    {
        if (strpos($role, ',')) {
            $roles = explode(" ", $role);
        } else {
            $roles = $role;
        }

        $menus = array();
        if (is_array($roles)) {
            $conarr = array();
            $conarr['in'] = array('role_id', $roles);
            $datas = $this->get_list_full($conarr);
            foreach ($datas as $data) {
                if ($data['menu_data'] != "") {
                    $menus = array_merge($menus, explode(',', $data['menu_data']));
                }
            }
        } else {
            $datas = $this->get_single(array("role_id" => $roles));
            if ($datas['menu_data'] != "") {
                $menus = explode(',', $datas['menu_data']);
            }

        }

        $menus = array_unique($menus);
        $ret = array();
        foreach ($menus as $menu) {
            array_push($ret, $menu);
        }
        return $ret;
    }

    /**
     * updateMenusByRole function
     *
     * @Name updateMenusByRole
     * @Desc 更新当前菜单权限表
     * @Author Pekey
     * @Date 2020-08-26
     * @param [type] $data
     * @param [type] $role_id
     * @return void
     */
    public function updateMenusByRole($data, $role_id)
    {
        return $this->update($data, array("role_id" => $role_id));
    }

}
